package taller2.view.components;

import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import taller2.interfaces.AlgorithmApplicator;
import taller2.utils.Constants;
import taller2.utils.Utils;
import taller2.view.windows.MyWindow;

public class CharacterBox extends JPanel{

	private static final long serialVersionUID = 205L;

	private AlgorithmApplicator algorithmApplicator;
    private MyWindow window;
    
    private JTextArea textArea;
    private MyHighlighter highlighter;
    private JScrollPane scrollBar;
    private int fontSize;
    
    public CharacterBox (AlgorithmApplicator algorithmApplicator, MyWindow window){
        
    	this.fontSize = (int)(Constants.fontSize*1.5);
        createComponents();
        
        this.algorithmApplicator = algorithmApplicator;
        this.window = window;
    }
    
    // creates and configures the textArea.
    private void createComponents(){
    	textArea = new JTextArea ("Inicia el algoritmo");
        textArea.setMargin(new Insets(0,0,0,0));
        textArea.setEditable(false);
        textArea.setFont(new Font("Serif",Font.PLAIN,fontSize));
        highlighter = new MyHighlighter(textArea);
        
        add(textArea);

        scrollBar = new JScrollPane(textArea);
        scrollBar.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);                  
        
        setLayout(new GridBagLayout());
        Utils.addComponent(this,scrollBar,0.2,1,new taller2.utils.Point(0,0));
    
    }
    
    public void update(){
    	highlighter.unHighlight();
    	
    	if(algorithmApplicator == null){
    		textArea.setText("No hay aplicador de algoritmo");
    	}else{
    		//el code browser llama a nextStep() del aplicador
    		textArea.setText(algorithmApplicator.getAuxText());    
    		
    		int higlightedWordNumber = algorithmApplicator.getHighlightedWordNumber();
    		if(higlightedWordNumber >= 0)
    			highlighter.highlightWord(higlightedWordNumber);
    	}
    	
    	window.redraw();      
    }

}
